package com.alstudio.module.xmpp.protocol.xmpp.parser.iq;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.text.TextUtils;

import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQType;
import com.alstudio.utils.xml.BaseXmlParser;
import com.alstudio.utils.xml.BaseXmlParser.XmlParseCallback;

public class IQParseEventHandler implements XmlParseCallback {

	private IQXmlParseEventCallback mCallback;
	private BaseXmlParser mParser;
	private ALIQType mType;

	public IQParseEventHandler(BaseXmlParser parser, ALIQType type,
			IQXmlParseEventCallback callback) {
		// TODO Auto-generated constructor stub
		mParser = parser;
		mType = type;
		mCallback = callback;
	}

	public void setIQParseEventCallback(IQXmlParseEventCallback callback) {
		mCallback = callback;
	}

	public String processText(String tag) {
		String text = null;
		try {
			text = mParser.readText();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return text;
	}

	public String getAttValue(String att) {
		if (TextUtils.isEmpty(att)) {
			throw new IllegalArgumentException("att should not null");
		}
		String value = "";
		value = mParser.getAttValue(att);

		return value;
	}

	/**
	 * 获取当前节点的所有属性
	 * 
	 * @param tag
	 * @return
	 */
	public ArrayList<String> getAttributes(String tag) {
		ArrayList<String> keys = null;
		try {
			keys = mParser.getAttributes(tag);
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return keys;
	}

	public void startParse() throws XmlPullParserException, IOException {
		mParser.startParse();
	}

	@Override
	public void onTagStart(String tag) {
		// TODO Auto-generated method stub
		switch (mType) {
		case ERROR:
			if (mCallback != null) {
				mCallback.processErrorStartTag(tag);
			}
			break;
		case GET:
			if (mCallback != null)
				mCallback.processGetStartTag(tag);
			break;
		case SET:
			if (mCallback != null)
				mCallback.processSetStartTag(tag);
			break;
		case RESULT:
			if (mCallback != null) {
				mCallback.processResultStartTag(tag);
			}
			break;
		}
	}

	@Override
	public void onTagEnd(String tag) {
		// TODO Auto-generated method stub
		if (mCallback != null) {
			mCallback.processEndTag(tag);
		}
	}

	@Override
	public void onDocumentStart() {
		// TODO Auto-generated method stub
		if (mCallback != null) {
			mCallback.processStartDocument();
		}
	}

	@Override
	public void onDocumentEnd() {
		// TODO Auto-generated method stub
		if (mCallback != null) {
			mCallback.processEndDocument();
		}
	}

	public interface IQXmlParseEventCallback {
		public void processErrorStartTag(String tag);

		public void processSetStartTag(String tag);

		public void processGetStartTag(String tag);

		public void processResultStartTag(String tag);

		public void processEndTag(String tag);

		public void processStartDocument();

		public void processEndDocument();
	}

}
